/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utils;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;
import kokonut.shop.color.Color;
import kokonut.shop.color.ExistingColors;
import kokonut.shop.product.Image;
import kokonut.shop.product.Model;
import kokonut.shop.product.TShirtModel;
import kokonut.shop.size.ExistingSizes;
import kokonut.shop.size.Size;
import persistence.PMF;

/**
 *
 * @author markel
 */
public class ProductEditor {

    private static BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

    public static String uploadImageToBlobstore(HttpServletRequest req, BlobKey blobKey){

        if (blobKey != null) {
            return blobKey.getKeyString();
        }
        else
            return null;
    }

    public static ArrayList<Size> assignPriceToSizes(ArrayList<Size> availableSizes, HttpServletRequest req) {
        for (int i = 0; i < availableSizes.size(); i++) {
            Size size = availableSizes.get(i);
            if (size.getSize().name().equals("S")) {
                float price = Float.parseFloat(req.getParameter("priceS"));
                size.setPrice(price);
                availableSizes.set(i, size);
            }
            if (size.getSize().name().equals("M")) {
                float price = Float.parseFloat(req.getParameter("priceM"));
                size.setPrice(price);
                availableSizes.set(i, size);
            }
            if (size.getSize().name().equals("L")) {
                float price = Float.parseFloat(req.getParameter("priceL"));
                size.setPrice(price);
                availableSizes.set(i, size);
            }
            if (size.getSize().name().equals("XL")) {
                float price = Float.parseFloat(req.getParameter("priceXL"));
                size.setPrice(price);
                availableSizes.set(i, size);
            }
            if (size.getSize().name().equals("XXL")) {
                float price = Float.parseFloat(req.getParameter("priceXXL"));
                size.setPrice(price);
                availableSizes.set(i, size);
            }
        }
        return availableSizes;
    }

    @SuppressWarnings("unchecked")
    public static boolean unusedReference(int ref) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        String query = "select from " + TShirtModel.class.getName() + " where ref == " + ref;
        try {
            List<Model> productList = (List<Model>) pm.newQuery(query).execute();
            if (productList.isEmpty()) {
                return true;
            } else {
                return false;
            }
        } catch (NullPointerException ex) {
            return true;
        } finally {
            pm.close();
        }
    }

    public static ArrayList<Color> createColors(String[] colors) {
        ArrayList<Color> availableColors = new ArrayList<Color>();
        try {
            for (String str : colors) {
                if (str.equals("BLACK")) {
                    availableColors.add(new Color(ExistingColors.BLACK));
                }
                if (str.equals("BLUE")) {
                    availableColors.add(new Color(ExistingColors.BLUE));
                }
                if (str.equals("BROWN")) {
                    availableColors.add(new Color(ExistingColors.BROWN));
                }
                if (str.equals("GREEN")) {
                    availableColors.add(new Color(ExistingColors.GREEN));
                }
                if (str.equals("GREY")) {
                    availableColors.add(new Color(ExistingColors.GREY));
                }
                if (str.equals("ORANGE")) {
                    availableColors.add(new Color(ExistingColors.ORANGE));
                }
                if (str.equals("PINK")) {
                    availableColors.add(new Color(ExistingColors.PINK));
                }
                if (str.equals("PURPLE")) {
                    availableColors.add(new Color(ExistingColors.PURPLE));
                }
                if (str.equals("RED")) {
                    availableColors.add(new Color(ExistingColors.RED));
                }
                if (str.equals("WHITE")) {
                    availableColors.add(new Color(ExistingColors.WHITE));
                }
                if (str.equals("YELLOW")) {
                    availableColors.add(new Color(ExistingColors.YELLOW));
                }
            }
            return availableColors;
        } catch (NullPointerException ex) {
            return null;
        }
    }

    public static ArrayList<Size> createSizes(String[] sizes) {

        ArrayList<Size> availableSizes = new ArrayList<Size>();
        try {
            for (String str : sizes) {
                if (str.equals("S")) {
                    availableSizes.add(new Size(ExistingSizes.S));
                }
                if (str.equals("M")) {
                    availableSizes.add(new Size(ExistingSizes.M));
                }
                if (str.equals("L")) {
                    availableSizes.add(new Size(ExistingSizes.L));
                }
                if (str.equals("XL")) {
                    availableSizes.add(new Size(ExistingSizes.XL));
                }
                if (str.equals("XXL")) {
                    availableSizes.add(new Size(ExistingSizes.XXL));
                }
            }
            return availableSizes;
        } catch (NullPointerException ex) {
            return null;
        }
    }

    public static ArrayList<Image> assignImages(HttpServletRequest req, ArrayList<Color> availableColors) {

        Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
        ArrayList<Image> images = new ArrayList<Image>();
        for(Color color: availableColors)
        {
            String consult = color.getColorName()+"Image";
            BlobKey blobKey = blobs.get(consult);
            String imageKey = uploadImageToBlobstore(req, blobKey);
            images.add(new Image(imageKey, color.getColorName()));
        }
        return images;
    }
}
